package com.pets.service.Impl;

import com.pets.dao.PhonecodeMapper;
import com.pets.dao.UsersMapper;
import com.pets.entity.Phonecode;
import com.pets.entity.Users;
import com.pets.enums.YesNoEnum;
import com.pets.qo.ResetPasswordQO;
import com.pets.qo.UserLoginQO;
import com.pets.qo.UserRegisterQO;
import com.pets.service.UserService;
import com.pets.util.CertificationUtil;
import com.pets.util.MD5Util;
import com.pets.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户相关service
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersDao;

    @Autowired
    private PhonecodeMapper phonecodeDao;

    /**
     * 用户登录
     * @param request
     * @param userLoginQO
     * @return
     */
    @Override
    public Result userLogin(HttpServletRequest request, UserLoginQO userLoginQO) {
        Result result = new Result();
        Users users = null;
        //登录可以通过用户名登录，通过用户名跟密码判断是否存在用户
        users = usersDao.getUsersByName(userLoginQO.getLoginName());
        if (users == null) {
            //登录可以通过手机号登录
            users = usersDao.getUsersByPhone(userLoginQO.getLoginName());
        }
        if (users != null) {
            if(users.getUstatus() == 1){
                result.setCode(YesNoEnum.NO.getCode());
                result.setMsg("登录失败，该账号已被禁用，请联系管理员！");
            }else if (MD5Util.getEncodeByMd5(userLoginQO.getPassword()).equals(users.getUpassword())) {
                request.getSession().setAttribute("user", users);
                result.setCode(YesNoEnum.YES.getCode());
                result.setMsg("登录成功");
            } else {
                result.setCode(YesNoEnum.NO.getCode());
                result.setMsg("登录失败，账号或密码有误请重新输入！");
            }
        } else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("登录失败，账号不存在请先注册！");
        }
        return result;
    }

    /**
     * 校验手机号是否被注册
     * @param phone
     * @return
     */
    @Override
    public Result checkPhone(String phone) {
        Result result = new Result();
        Users users = usersDao.getUsersByPhone(phone);
        //users为空代表可以注册，返回成功
        if (users == null) {
            result.setMsg("可以注册");
            result.setCode(YesNoEnum.YES.getCode());
        } else {
            //users不为空，代表不能注册，返回失败
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("该手机号已经被注册！");
        }
        return result;
    }

    /**
     * 校验用户名是否被注册
     * @param uName
     * @return
     */
    @Override
    public Result checkUserName(String uName) {
        Result result = new Result();
        Users users = usersDao.getUsersByName(uName);
        //users为空代表可以注册，返回成功
        if (users == null) {
            result.setMsg("可以注册");
            result.setCode(YesNoEnum.YES.getCode());
        } else {
            //users不为空，代表不能注册，返回失败
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("该用户名已经被注册！");
        }
        return result;
    }

    /**
     * 用户注册
     * @param userRegisterQO
     * @return
     */
    @Override
    public Result userRegister(UserRegisterQO userRegisterQO) {
        Result result = new Result();
        //校验手机验证码是否正确
        Phonecode phonecode = phonecodeDao.selectPhoneCodeByPhoneAndCode(userRegisterQO.getPhone(), userRegisterQO.getCode());
        if (phonecode == null || phonecode.getId() == null) {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("验证码错误");
            return result;
        }
        //执行注册操作
        Users users = new Users();
        users.setUname(userRegisterQO.getUserName());
        users.setUpassword(MD5Util.getEncodeByMd5(userRegisterQO.getPassword()));
        users.setUphone(userRegisterQO.getPhone());
        users.setUstatus(0);
        users.setUtype(1);
        int i = usersDao.insert(users);
        if(i >0){
           result.setCode(YesNoEnum.YES.getCode());
           result.setMsg("注册成功");
           //删除手机验证码
            phonecodeDao.deleteByPrimaryKey(phonecode.getId());
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("注册失败");
            return result;
        }
    }

    /**
     * 重置密码 查看该手机号是否存在
     * @param phone
     * @return
     */
    @Override
    public Result resetPasswordGetUserByPhone(String phone) {
        Result result = new Result();
        Users users = usersDao.getUsersByPhone(phone);
        //users不为空代表可以重置密码，返回成功
        if (users != null) {
            result.setMsg("可以重置密码");
            result.setCode(YesNoEnum.YES.getCode());
        } else {
            //users为空，代表不能重置密码，返回失败
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("该手机号未注册");
        }
        return result;
    }

    /**
     * 重置密码
     * @param resetPasswordQO
     * @return
     */
    @Override
    public Result resetPassword(ResetPasswordQO resetPasswordQO) {
        Result result = new Result();
        //校验手机验证码是否正确
        Phonecode phonecode = phonecodeDao.selectPhoneCodeByPhoneAndCode(resetPasswordQO.getPhone(), resetPasswordQO.getCode());
        if (phonecode == null || phonecode.getId() == null) {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("验证码错误");
            return result;
        }
        //校验用户是否存在[在进行一次校验，避免下面的user出现空指针异常]
        Users users = usersDao.getUsersByPhone(resetPasswordQO.getPhone());
        if(users == null){
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("手机号未被注册");
            return result;
        }
        //执行修改密码
        users.setUpassword(MD5Util.getEncodeByMd5(resetPasswordQO.getPassword()));
        int i = usersDao.updateByPrimaryKey(users);
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("重置密码成功");
            //删除手机验证码
            phonecodeDao.deleteByPrimaryKey(phonecode.getId());
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("重置密码失败");
            return result;
        }
    }

    @Override
    public Users getUsersById(int userId) {

        return usersDao.selectByPrimaryKey(userId);
    }

    /**
     * 修改用户密码
     * @param userId
     * @param password
     * @return
     */
    @Override
    public Result userUpdatePassword(int userId, String password) {

        Result result = new Result();
        //根据用户id获取用户信息
        Users users = usersDao.selectByPrimaryKey(userId);
        //比对是否与原密码一样，判断加密前是否有密码的修改
        if(users.getUpassword().equals(password)){
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("修改失败，密码与原密码一样");
            return result;
        }
        //比对加密后是否与原密码一样，判断输入重置密码后加密后是否一样
        if(users.getUpassword().equals(MD5Util.getEncodeByMd5(password))){
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("修改失败，密码与原密码一样");
            return result;
        }
        users.setUpassword(MD5Util.getEncodeByMd5(password));
        int i = usersDao.updateByPrimaryKey(users);
        if(i <= 0){
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("修改失败");
            return result;
        }
        result.setMsg("修改成功");
        result.setCode(YesNoEnum.YES.getCode());
        return result;
    }

    /**
     * 实名认证
     * @param userId
     * @param cardNo
     * @param realName
     * @return
     */
    @Override
    public Result realNameCertification(int userId, String cardNo, String realName) {
        Result result = new Result();
        //调用实名认证
       boolean flag = CertificationUtil.aLiCertification(cardNo,realName);
       if(!flag){
           result.setCode(YesNoEnum.NO.getCode());
           result.setMsg("实名认证失败");
           return result;
       }
        //根据用户id获取用户信息
        Users users = usersDao.selectByPrimaryKey(userId);
        users.setUrealname(realName);
        users.setUidnumber(cardNo);
        int i = usersDao.updateByPrimaryKey(users);
        if(i <= 0){
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("实名认证失败");
            return result;
        }
        result.setCode(YesNoEnum.YES.getCode());
        result.setMsg("实名认证成功");
        return result;
    }
}
